<template>
    <div class="chat">
        <van-nav-bar fixed placeholder title="客服" left-arrow @click-left="fh" />
        <div id="content" ref="rightBody">
            <div style="text-align: center; color: #787878; font-size: 13px; margin-top: 10px">
                <span v-show="show" @click="fy" class="check">点击查看更多</span>
            </div>
            <div v-for="item in list" :key="item.id">
                <div class="chat-model" style="justify-content: flex-end" v-if="item.isself">
                    <div style="position: absolute; right: 50px; top: -3px">
                        <span style="font-size: 10px; color: #a9a9a9">{{ item.send_time }}</span>
                    </div>
                    <div class="chat-content chat-content-r">
                        <div v-if="!item.flag">
                            <span >{{ item.content }}</span>
                            <span v-if="item.image">
                                <img :src="item.image" alt="">
                            </span>
                        </div>
                        <!-- 当点击的是商品详情，那么发送的内容便是这个 -->
                        <!-- <div v-else>
                            <div style="width: 200px;">
                                <div style="font-weight: 800;font-size:13px">你正在咨询的订单</div>
                                <div style="background-color:#e9e9e9;display: flex;margin-top: 5px;">
                                    <div style="padding:3px">
                                        <img style="width: 30px;height:30px"
                                            src="https://gimg3.baidu.com/search/src=http%3A%2F%2Fpics3.baidu.com%2Ffeed%2F9825bc315c6034a8d1e4b68f5c36725f0923766e.jpeg%40f_auto%3Ftoken%3De3d3b6a6da83ef2a5beaae8c33bc4550&refer=http%3A%2F%2Fwww.baidu.com&app=2021&size=f360,240&n=0&g=0n&q=75&fmt=auto?sec=1668099600&t=fef166daf649df58626d5c8dfade2c84"
                                            alt="">
                                    </div>
                                    <div style="line-height: 40px;margin-left: 5px;color:#989898;font-size: 13px">java课程
                                    </div>
                                </div>
                                <div
                                    style="display: flex; justify-content: space-between; font-size: 12px;margin-top: 5px;">
                                    <span style="color:#989898">订单号:</span>
                                    <span>132456</span>
                                </div>
                                <div
                                    style="display: flex; justify-content: space-between; font-size: 12px;margin-top: 5px;">
                                    <span style="color:#989898">创建时间:</span>
                                    <span>123456</span>
                                </div>
                            </div>
                        </div> -->
                    </div>
                    <div>
                        <van-image width="45px" height="45px" fit="fill" round :src="item.sender_avatar" />
                    </div>
                </div>
                <div class="chat-model" v-else>
                    <div>
                        <van-image width="45px" height="45px" fit="fill" round :src="item.sender_avatar" />
                    </div>
                    <div>
                        <div style="margin-bottom: -10px">
                            <span style="font-size: 10px">{{ item.sender_nickname }}</span
                            ><span style="font-size: 10px; margin-left: 10px; color: #a9a9a9">{{ item.send_time }}</span>
                        </div>
                        <div class="chat-content chat-content-l">
                            {{ item.content }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div></div>

        <div style="text-align: center; color: #787878; font-size: 13px">
            <span v-show="latelyshow" @click="lately" class="btn">点击查看最近消息</span>
        </div>
        <div id="bottom">
            <div id="left">
                <input type="text" v-model="text" />
                <van-button @click="onSend('send')">发送</van-button>
                <div id="icon" @click="isActive = !isActive">
                    <van-icon name="add-o" />
                </div>
            </div>
            <div v-show="isActive" class="box" style="display: flex">
                <div style="margin: 10px; display: flex; flex-direction: column; justify-content: space-evenly; align-items: center">
                    <van-uploader
                        :max-count="1"
                        accept="image/*"
                        :after-read="afterRead"
                        :before-delete="deleteFile"
                        :max-size="2 * 1024 * 1024"
                    >
                        <div style="border-radius: 1px; background-color: #fff; height: 50px; width: 50px; text-align: center">
                            <van-icon class="wy" name="photo" />
                        </div>
                    </van-uploader>
                    <span style="font-size: 10px; color: darkgray">图片</span>
                </div>

                <div style="margin: 10px; display: flex; flex-direction: column; justify-content: space-evenly; align-items: center">
                    <div style="border-radius: 1px; background-color: #fff; height: 50px; width: 50px; text-align: center">
                        <van-icon class="wy" name="bag" />
                    </div>
                    <span style="font-size: 10px; color: darkgray" @click="onSend('shop')">商品详情</span>
                </div>
            </div>
        </div>
    </div>
</template>
  
<script>
import frontUploadFileMixin from '@/mixin/frontuploadossfile.js';
import frontDeleteFileMixin from '@/mixin/frontdeleteossfile.js';
export default {
    name: 'HelloWorld',
    data() {
        return {
            image:"",
            latelyshow: false,
            show: false,
            text: '',
            list: [],
            isActive: false,
            orderid: '',
            stime: '',
            limit: 10,
            page: 1,
            total: 0,
            messagenumber: 10
        };
    },
    mixins: [frontUploadFileMixin, frontDeleteFileMixin],
    async created() {
        await this.getData();
        this.send();
        if (this.total > this.messagenumber) {
            this.show = true;
        }
    },
    methods: {
        async deleteFile(file) {
            this.deletefrontMultiFile([file.path]);
        },
        async afterRead(file) {
            if (!Array.isArray(file)) {
                file = [file];
            }
            let shopname = localStorage.getItem('shopname');
            let phone = localStorage.getItem('phone');
            let path = 'image/avatar/' + phone + '/' + shopname + '/';
            console.log(phone, shopname);
            for (let i = 0; i < file.length; i++) {
                let fileUrl = file[i].file;
                file[i].status = 'uploading';
                // file[i].message = '上传中...';
                console.log(fileUrl, path + file[i].file.name);
                let url = await this.frontFileUpload(fileUrl, path + file[i].file.name, i, this, this.$front.frontuserbucket); //上传到oss
                console.log(url);
                file[i].url = url;
                file[i].status = 'done';
                // file[i].message = '上传成功'
                file[i].path = path + file[i].file.name;
                this.image = url;
                this.onSend('uploading');
            }
            // console.log(this.fileList);
        },
        async getData() {
            this.orderid = this.$route.query.orderid;
            let res = await this.$frontaxios.post(this.$front.getChatContent, {
                orderid: this.orderid,
                limit: this.limit,
                page: this.page
            });
            if (res.code == 1) {
                this.list = res.data.chatdata.reverse();
                this.total = res.data.total;
            }
            return;
        },
        async onSend(a) {
            if (a == 'send') {
                if (!this.text) {
                    this.$toast('请输入内容');
                    return;
                }
                console.log(111)
                var data = {
                    orderid: this.orderid,
                    content: this.text,
                    isself: 1
                };
                let res = await this.$frontaxios.post(this.$front.sendKeFuChat, data);
                if (res.code == 1) {
                    this.text = '';
                    this.$refs.rightBody.scrollTop = this.$refs.rightBody.scrollHeight - this.$refs.rightBody.clientHeight;
                    this.list.push(data);
                    this.getData();
                }
            }  else if (a == 'uploading') {
                console.log(111)
                var data = {
                    orderid: this.orderid,
                    image: this.image,
                    isself: 1
                };
                let res = await this.$frontaxios.post(this.$front.sendKeFuChat, data);
                if (res.code == 1) {
                    this.text = '';
                    this.$refs.rightBody.scrollTop = this.$refs.rightBody.scrollHeight - this.$refs.rightBody.clientHeight;
                    this.list.push(data);
                    this.getData();
                }
            }else {
                //当点击的是商品详情的时候发送的

                var data = {
                    orderid: this.orderid,
                    content: this.text,
                    isself: true
                };
                console.log(data);
                let res = await this.$frontaxios.post(this.$front.sendKeFuChat, data);
            }
        },
        send() {
            this.stime = setInterval(() => {
                this.getData();
            }, 5000);
        },
        fh() {
            clearInterval(this.stime);
            this.$router.back();
        },
        async fy() {
            this.page++;
            this.latelyshow = true;
            let res = await this.$frontaxios.post(this.$front.getChatContent, {
                orderid: this.orderid,
                limit: this.limit,
                page: this.page
            });
            if (res.code == 1) {
                this.list = res.data.chatdata.reverse();
                this.total = res.data.total;
            }
            if (this.list.length < this.messagenumber) {
                this.show = false;
            }
        },
        async lately() {
            this.page = 1;
            this.latelyshow = false;
            this.show = true;
            let rst = await this.$frontaxios.post(this.$front.getChatContent, {
                orderid: this.orderid,
                limit: this.limit,
                page: this.page
            });
            if (rst.code == 1) {
                this.list = rst.data.chatdata.reverse();
                this.total = rst.data.total;
            }
        }
    },

    destroyed() {
        // 销毁监听
        clearInterval(this.stime);
    }
};
</script>
  
<style>
.chat {
    height: 100vh;
    background-color: #f1f1f3;
}

#content {
    overflow: auto;
    height: calc(100vh - 102px);
    /* padding-bottom: 300px; */
    background-color: #f1f1f3;
}

#bottom {
    position: fixed;
    width: 100%;
    bottom: 0px;
    /* display: flex;
    justify-content: space-evenly;
    padding: 10px 0px; */
    background-color: #f1f1f3;
}

#left {
    width: 100%;
    display: flex;
    justify-content: space-evenly;
    padding: 10px 0px;
    background-color: #f1f1f3;
}

#bottom input {
    background-color: white;
    width: 72%;
    height: 30px;
    padding: 3px 5px;
    vertical-align: sub;
    border-style: none;
    border-radius: 5px;
}

#bottom button {
    height: 32px;
    background-color: rgb(245, 158, 1);
    border-radius: 5px;
    color: #fff;
}

.chat-model {
    display: flex;
    flex-direction: row;
    margin: 10px 10px;
    margin-top: 30px;
    align-items: center;
    position: relative;
}

.chat-content {
    position: relative;
    max-width: 67%;
    word-break: break-all;
    word-wrap: break-word;
    top: 18px;
    padding: 10px;
    border-radius: 5px;
    background-color: white;
}

.chat-content-r {
    right: 10px;
}

.chat-content-l {
    left: 10px;
}

#icon {
    font-size: 25px;
    margin-top: 2px;
}

.box {
    width: 100%;
    height: 100px;
}

.box .van-icon {
    font-size: 40px;
    color: dimgray;
}

.check {
    margin: 0 auto;
    margin-top: 10px;
}

.btn {
    left: 40%;
    position: fixed;
    bottom: 60px;
}
</style>